<template>
	<view class="confirm_order">
		<uv-navbar placeholder autoBack bgColor="#CA1C1D">
			<template v-slot:left>
				<view class="left_slot">
					<image src="@/static/images/back.png"></image>
				</view>
			</template>
			<template v-slot:center>
				<view class="nav_title">确认订单</view>
			</template>
		</uv-navbar>

		<scroll-view scroll-y class="scroll_view">
			<view class="address" @click="router('/pages/my/address?is=1')">
				<image src="@/static/images/zb.png" class="zb"></image>
				<view class="address_view">
					<view class="address_info">
						<view class="name">{{region.name}}</view>
						<view class="phone">{{region.phone}}</view>
					</view>
					<view class="address_deta">{{region.region_text}}</view>
				</view>
				<image src="@/static/images/r1.png" class="next"></image>
			</view>
			<view class="goods_info">
				<view class="goods_box" v-for="item in list">
					<image :src="item.product_img" class="cover"></image>
					<view class="goods_view">
						<view class="goods_deta">
							<view class="name">{{item.product_name}}</view>
							<view class="synopsis">{{item.synopsis}}</view>
						</view>
						<view class="price">
							<view class="unit">￥</view>
							<view class="value">{{item.price}}</view>
						</view>
					</view>
				</view>
				<view class="leave">
					<view class="key">留言</view>
					<input type="text" v-model="form.message" placeholder="请输入您的留言">
				</view>
			</view>
			<view class="cell_view">
				<!-- <view class="item">
					<view class="key">优惠</view>
					<view class="value red">使用优惠券 -￥10</view>
					<image src="@/static/images/c13.png" class="icon"></image>
				</view> -->
				<view class="item">
					<view class="key">运费</view>
					<view class="value" v-if="data_info.freight">￥{{data_info.freight}}</view>
					<view class="value" v-else>包邮</view>
				</view>
				<view class="item">
					<view class="key">积分</view>
					<view class="value">购买赠送{{data_info.integral}}积分</view>
				</view>
				<view class="item">
					<view class="key">消费券:{{ data_info.shopping }}</view>
					<view class="value">
						<uv-switch v-model="use_shopping" size="20" activeColor="#CA1C1D" @change="change"></uv-switch>
					</view>
				</view>
			</view>
		</scroll-view>

		<view class="footer">
			<view class="left">
				<view class="key">共{{data_info.number_all}}件商品 合计：</view>
				<view class="pricer">
					<view class="unit">￥</view>
					<view class="value"> {{data_info.real_total}}</view>
				</view>
			</view>
			<view class="right" @click="open">提交订单</view>
			<!-- <view class="right" @click="submit">提交订单</view> -->
		</view>
		<uv-popup ref="paymentRef" mode="bottom">
			<Payment @close="paymentRef.close()" @submit="submit" />
		</uv-popup>
	</view>
</template>

<script setup>
	import {
		orderConfirm,
		orderPlace,
		orderPayment
	} from "@/request/api";

	import Payment from "@/components/common/Payment.vue";

	import useWechat from "@/hooks/useWechat.js";
	import useBase from "@/hooks/useBase.js";
	const {
		wxPay
	} = useWechat();
	const {
		router
	} = useBase();
	import {
		onLoad,
		onShow,
		onUnload
	} from "@dcloudio/uni-app";
	import {
		ref,
		reactive,
		shallowRef
	} from "vue";
	import {
		storeToRefs
	} from 'pinia';
	import useStore from "@/stores/index.js";
	const {
		useAddressStore
	} = useStore();
	let {
		address
	} = storeToRefs(useAddressStore);

	const data_info = ref({
		freight: 0.00,
		integral: 0.00,
		number_all: 0,
		total: 0.00,
		real_total: 0.00,
		shopping: 0.00,
	});
	const form = reactive({
		users_underwrite_id: "",
		product_id: "",
		address_id: "",
		number: "",
		message: "",
		use_shopping: 0
	});

	let use_shopping = ref(false);
	const change = () => {
		let value = use_shopping.value;
		if (value) {
			let val = data_info.value.total - data_info.value.shopping;
			val = val <= 0 ? 0 : val;
			data_info.value.real_total = val;
		} else {
			data_info.value.real_total = data_info.value.total;
		};
	};

	let order_id;
	const paymentRef = shallowRef();
	const open = async () => {
		form.use_shopping = use_shopping.value ? 1 : 0;
		let res = await orderPlace(form);
		order_id = res.order_id;
		let real_total = res.real_total;

		if (Number(real_total) <= 0) {
			uni.$uv.toast("操作成功");
			setTimeout(() => {
				uni.switchTab({
					url: '/pages/tabbar/my'
				})
			}, 1000);
			return false;
		};
		paymentRef.value.open();
		// paymentRef.value.open();
	};
	const submit = async (pay_mode) => {
		let {
			pay_data
		} = await orderPayment({
			pay_mode,
			id: order_id
		});
		if (pay_mode == 2) {
			uni.$uv.toast("操作成功");
			setTimeout(() => {
				uni.switchTab({
					url: '/pages/tabbar/my'
				})
			}, 1000);
			return false;
		};
		wxPay(pay_data, () => {
			uni.$uv.toast("操作成功");
			setTimeout(() => {
				uni.switchTab({
					url: '/pages/tabbar/my'
				})
			}, 1000);
		});
	};
	// const submit = async () => {
	// form.use_shopping = form.use_shopping ? 1 : 0;
	// let {
	// 	order_id
	// } = await orderPlace(form);

	// let {
	// 	pay_data
	// } = await orderPayment({
	// 	id: order_id
	// });

	// wxPay(pay_data, () => {
	// 	uni.$uv.toast("操作成功");
	// 	setTimeout(() => {
	// 		uni.switchTab({
	// 			url: '/pages/tabbar/my'
	// 		})
	// 	}, 1000);
	// });
	// };
	const region = ref({
		name: "",
		phone: "",
		region_text: "",
	});
	const list = ref([]);
	const getDeta = async () => {
		let res = await orderConfirm({
			product_id: form.product_id,
			number: form.number,
			users_underwrite_id: form.users_underwrite_id
		});

		list.value = res.order.product;
		data_info.value = {
			...res.order
		};

		data_info.value.real_total = data_info.value.total;
		data_info.value.shopping = res.shopping;
		change();

		if (!address.value) {
			address.value = res.address;
		}
		if (!address.value) return false;

		let {
			full_address,
			name,
			phone
		} = address.value;
		form.address_id = address.value.id;
		region.value.region_text = full_address;
		region.value.name = name;
		region.value.phone = phone;
	};
	onLoad(options => {
		form.users_underwrite_id = options.uid;
		form.product_id = options.id;
		form.number = options.number;
	});
	onShow(() => getDeta());
	onUnload(() => {
		paymentRef.value.close();
		useAddressStore.removeAddress();
	});
</script>

<style scoped lang="scss">
	.confirm_order {
		height: calc(100vh - var(--window-top) - var(--window-bottom));
		background-color: #f5f5f5;

		.left_slot {
			image {
				width: 43rpx;
				height: 36rpx;
			}
		}

		.nav_title {
			color: #F5F5F5;
			font-size: 36rpx;
		}

		.scroll_view {
			height: calc(100vh - var(--window-top) - var(--window-bottom) - #{$navBarHeight} - 100rpx);
			padding: 20rpx 30rpx;
			box-sizing: border-box;

			.address {
				border-radius: 16rpx;
				padding: 44rpx 30rpx;
				box-sizing: border-box;
				background: #FFFFFF;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.zb {
					width: 70rpx;
					height: 70rpx;
				}

				.address_view {
					flex: 1;
					height: 70rpx;
					padding: 0 20rpx;
					box-sizing: border-box;
					overflow: hidden;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.address_info {
						display: flex;
						align-items: center;

						.name {
							color: #333333;
							font-size: 30rpx;
							font-weight: bold;
						}

						.phone {
							color: #999999;
							font-size: 24rpx;
							padding-left: 30rpx;
							box-sizing: border-box;
						}
					}

					.address_deta {
						color: #333333;
						font-size: 24rpx;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
					}
				}

				.next {
					width: 19rpx;
					height: 30rpx;
				}
			}

			.goods_info {
				border-radius: 16rpx;
				margin: 20rpx 0;
				padding: 30rpx 30rpx 0;
				box-sizing: border-box;
				background: #FFFFFF;
				overflow: hidden;

				.goods_box {
					padding-bottom: 40rpx;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;

					.cover {
						width: 200rpx;
						height: 200rpx;
						border-radius: 10rpx;
					}

					.goods_view {
						flex: 1;
						padding-left: 20rpx;
						overflow: hidden;
						display: flex;
						flex-direction: column;
						justify-content: space-between;

						.goods_deta {
							.name {
								line-height: 36rpx;
								color: #333333;
								font-size: 30rpx;
								font-weight: bold;
								overflow: hidden;
								text-overflow: ellipsis;
								display: -webkit-box;
								-webkit-line-clamp: 2;
								-webkit-box-orient: vertical;
							}

							.synopsis {
								color: #999999;
								font-size: 20rpx;
								padding-top: 18rpx;
								box-sizing: border-box;
							}
						}

						.price {
							display: flex;
							align-items: center;

							.unit {
								color: #CA1C1D;
								font-size: 32rpx;
								font-weight: bold;
							}

							.value {
								color: #CA1C1D;
								font-size: 50rpx;
								font-weight: bold;
							}
						}
					}
				}

				.leave {
					border-top: 2rpx solid #EEEEEE;
					padding: 26rpx 0;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.key {
						color: #333333;
						font-size: 30rpx;
					}

					input {
						flex: 1;
						padding-left: 18rpx;
						box-sizing: border-box;
					}
				}
			}

			.cell_view {
				border-radius: 20rpx;
				background: #FFFFFF;
				overflow: hidden;

				.item {
					height: 88rpx;
					padding: 0 30rpx;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.key {
						color: #999999;
						font-size: 30rpx;
					}

					.value {
						flex: 1;
						color: #333333;
						font-size: 30rpx;
						padding-right: 18rpx;
						box-sizing: border-box;
						display: flex;
						justify-content: flex-end;

						&.red {
							color: #F34343;
						}
					}

					.icon {
						width: 20rpx;
						height: 20rpx;
					}
				}
			}
		}

		.footer {
			width: 100%;
			height: 100rpx;
			padding: 0 30rpx;
			box-sizing: border-box;
			background: #FFFFFF;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.left {
				display: flex;
				align-items: center;

				.key {
					color: #999999;
					font-size: 22rpx;
				}

				.pricer {
					display: flex;
					align-items: center;

					.unit {
						color: #CA1C1D;
						font-size: 32rpx;
						font-weight: bold;
					}

					.value {
						color: #CA1C1D;
						font-size: 50rpx;
						font-weight: bold;
					}
				}
			}

			.right {
				width: 214rpx;
				height: 72rpx;
				line-height: 72rpx;
				text-align: center;
				color: #FFFFFF;
				font-size: 26rpx;
				border-radius: 10rpx;
				background: #CA1C1D;
			}
		}
	}
</style>